/*
 * Part of the ACID Project
 * Developed in 2011 by Morgan Taschuk
 */
package acid.plugin;

import acid.plugin.gui.ACIDVisualStyle;
import cytoscape.Cytoscape;
import cytoscape.task.Task;
import cytoscape.task.TaskMonitor;
import cytoscape.view.CyNetworkView;
import cytoscape.visual.CalculatorCatalog;
import cytoscape.visual.VisualMappingManager;
import cytoscape.visual.VisualStyle;

/**
 *
 * @author morgan
 */
public class SetVisualStyle implements Task
{

    private TaskMonitor taskMonitor;

    private void setVisualStyle()
    {
        taskMonitor.setStatus("Searching for ACID visual style");
        // get the network and view
        CyNetworkView networkView = Cytoscape.getCurrentNetworkView();

        // get the VisualMappingManager and CalculatorCatalog
        VisualMappingManager manager = Cytoscape.getVisualMappingManager();
        CalculatorCatalog catalog = manager.getCalculatorCatalog();

        // check to see if a visual style with this name already exists
        VisualStyle vs = catalog.getVisualStyle(ACIDVisualStyle.vsName);
        if (vs == null)
        {
            taskMonitor.setStatus("Creating visual style...");
            // if not, create it and add it to the catalog
            vs = (new ACIDVisualStyle()).createVisualStyle();
            catalog.addVisualStyle(vs);
        }

        taskMonitor.setStatus("Applying visual style");
        networkView.setVisualStyle(vs.getName()); // not strictly necessary

        // actually apply the visual style
        manager.setVisualStyle(vs);

        taskMonitor.setStatus("Redrawing network");
        networkView.redrawGraph(true, true);
    }

    @Override
    public void run()
    {
        taskMonitor.setPercentCompleted(-1);

        taskMonitor.setStatus("Setting Visual Style...");
        setVisualStyle();

    }

    @Override
    public void halt()
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void setTaskMonitor(TaskMonitor monitor) throws IllegalThreadStateException
    {
        taskMonitor = monitor;
    }

    @Override
    public String getTitle()
    {
        return "Setting Visual Style";
    }
}
